Imports System.Security.Cryptography
Imports System.IO
Imports System.Text

Public Class Class_Encrypt
    ' Use DES CryptoService with Private key pair
    Private mSEncryptionKey As String = "Godzilla"
    Private mIV() As Byte = {&HEF, &HB3, &H91, &HAC, &H38, &HDD, &HC9, &H46}
    Private mKey() As Byte = {}

#Region " Public Properties "

    Public WriteOnly Property SEncryptionKey() As String
        Set(ByVal Value As String)
            mSEncryptionKey = Value
        End Set
    End Property

    Public WriteOnly Property IV() As Byte()
        Set(ByVal Value As Byte())
            mIV = Value
        End Set
    End Property

    'Public WriteOnly Property IV() As Byte()
    '    Set(ByVal Value As Byte())
    '        mIV = Value
    '    End Set
    'End Property

#End Region

#Region " Public Functions "

    Public Sub New()

    End Sub

    Public Sub New(ByVal nEncryptionKey As String, ByVal nIV As Byte())
        'SEncryptionKey = nEncryptionKey
        'IV = nIV
    End Sub

    Public Function Decrypt(ByVal stringToDecrypt As String) As String
        Dim inputByteArray(stringToDecrypt.Length) As Byte
        ' Note: The DES CryptoService only accepts certain key byte lengths
        ' We are going to make things easy by insisting on an 8 byte legal key length

        Try
            mkey = System.Text.Encoding.UTF8.GetBytes(Left(mSEncryptionKey, 8))
            Dim des As New DESCryptoServiceProvider()
            ' we have a base 64 encoded string so first must decode to regular unencoded (encrypted) string
            inputByteArray = Convert.FromBase64String(stringToDecrypt)
            ' now decrypt the regular string
            Dim ms As New MemoryStream()
            Dim cs As New CryptoStream(ms, des.CreateDecryptor(mkey, mIV), CryptoStreamMode.Write)
            cs.Write(inputByteArray, 0, inputByteArray.Length)
            cs.FlushFinalBlock()
            Dim encoding As System.Text.Encoding = System.Text.Encoding.UTF8
            Return encoding.GetString(ms.ToArray())
        Catch e As Exception
            Return e.Message
        End Try
    End Function

    Public Function Encrypt(ByVal stringToEncrypt As String) As String
        Try
            mkey = System.Text.Encoding.UTF8.GetBytes(Left(mSEncryptionKey, 8))
            Dim des As New DESCryptoServiceProvider()
            ' convert our input string to a byte array
            Dim inputByteArray() As Byte = Encoding.UTF8.GetBytes(stringToEncrypt)
            'now encrypt the bytearray
            Dim ms As New MemoryStream()
            Dim cs As New CryptoStream(ms, des.CreateEncryptor(mkey, mIV), CryptoStreamMode.Write)
            cs.Write(inputByteArray, 0, inputByteArray.Length)
            cs.FlushFinalBlock()
            ' now return the byte array as a "safe for XMLDOM" Base64 String
            Return Convert.ToBase64String(ms.ToArray())
        Catch e As Exception
            Return e.Message
        End Try
    End Function
    'Public Function Encrypting(ByVal Source As String) As String
    '    Dim bytIn As Byte() = System.Text.ASCIIEncoding.ASCII.GetBytes(Source)
    '    Dim ms As New System.IO.MemoryStream
    '    Dim mobjCryptoService As SymmetricAlgorithm
    '    mobjCryptoService = New DESCryptoServiceProvider()
    '    Dim bytKey() As Byte
    '    bytKey = GetLegalKey("Godzilla", mobjCryptoService)

    '    mobjCryptoService.Key = bytKey
    '    mobjCryptoService.IV = bytKey

    '    Dim encrypto As ICryptoTransform = mobjCryptoService.CreateEncryptor
    '    Dim cs As CryptoStream = New CryptoStream(ms, encrypto, CryptoStreamMode.Write)

    '    cs.Write(bytIn, 0, bytIn.Length)
    '    cs.FlushFinalBlock()
    '    Dim bytOut() As Byte = ms.GetBuffer
    '    Dim i As Integer = 0
    '    For i = 0 To bytOut.Length
    '        If bytOut(i) = 0 Then
    '            Exit For
    '        End If
    '    Next

    '    Return System.Convert.ToBase64String(bytOut, 0, i)
    'End Function

    'Private Function GetLegalKey(ByVal Key As String, ByRef mobjCryptoService As SymmetricAlgorithm) As Byte()
    '    Dim sTemp As String
    '    If mobjCryptoService.LegalKeySizes.Length > 0 Then
    '        Dim lessSize As Integer = 0
    '        Dim moresize As Integer = mobjCryptoService.LegalKeySizes(0).MinSize
    '        While Key.Length * 8 > moresize
    '            lessSize = moresize
    '            moresize += mobjCryptoService.LegalKeySizes(0).SkipSize
    '        End While
    '        sTemp = Key.PadRight(moresize / 8, " ")
    '    Else
    '        sTemp = Key
    '    End If
    '    Return ASCIIEncoding.ASCII.GetBytes(sTemp)
    'End Function

    'Public Function Decrypting(ByVal Source As String) As String
    '    Dim mobjCryptoService As SymmetricAlgorithm
    '    mobjCryptoService = New DESCryptoServiceProvider()
    '    Dim bytIn() As Byte = System.Convert.FromBase64String(Source)
    '    Dim ms As System.IO.MemoryStream = New System.IO.MemoryStream(bytIn, 0, bytIn.Length)
    '    Dim bytKey() As Byte = GetLegalKey("Godzilla", mobjCryptoService)
    '    mobjCryptoService.Key = bytKey
    '    mobjCryptoService.IV = bytKey

    '    Dim encrypto As ICryptoTransform = mobjCryptoService.CreateDecryptor
    '    Dim cs As CryptoStream = New CryptoStream(ms, encrypto, CryptoStreamMode.Read)
    '    Dim sr As System.IO.StreamReader = New System.IO.StreamReader(cs)

    '    Return sr.ReadToEnd
    'End Function
#End Region

End Class
